package com.yphone.ui.pay.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;

import com.yphone.net.IUIDataListener;
import com.yphone.net.bean.BaseResultWrapper;
import com.yphone.net.bean.base.BaseRequestInfo;
import com.yphone.tools.ToastUtil;
import com.yphone.ui.dialog.CommSmallLoadingDialog;
import com.yphone.ui.login.manager.LoginManager;
import com.yphone.ui.main.MainActivity;
import com.yphone.ui.widget.TopView;
import com.yphone.ui.ysj.bean.RemoteConnectBean;
import com.zzkj.yunphone.R;
import com.yphone.tools.MyLog;
import com.yphone.ui.event.MyEvent;
import com.yphone.ui.login.contract.LoginActivityContract;
import com.yphone.ui.pay.bean.PayInfo;
import com.yphone.ui.pay.contract.PayActivityContract;
import com.yphone.ui.pay.presenter.PayPresenter;
import com.yphone.ui.pay.view.PayItem;
import com.yphone.ui.pay.view.SelectNumLayout;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

public class PayActivity extends AppCompatActivity implements PayActivityContract.IView {

    private PayActivityContract.IPresenter mIPresenter;
    private GridView mGridview;
    private BaseAdapter mAdapter;
    private List<PayInfo> data = new ArrayList<>();
    private PayPresenter payPresenter;
    private SelectNumLayout select_num;
    private TextView tv_order_price;
    private PayInfo selectPayInfo;
    private Button btn_pay;
    private TopView inc_top_view;
    private View ll_renew;
    private View v_renew;
    private TextView tv_renew_name;
    private int mNum = 1;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        setContentView(R.layout.activity_pay);
        initView();
        initData();
        initListener();
    }

    public static void toPayActivity(Context context) {
        Intent intent = new Intent(context, PayActivity.class);
        context.startActivity(intent);
    }
    public static void toPayActivity(Context context,RemoteConnectBean bean) {
        Intent intent = new Intent(context, PayActivity.class);
        intent.putExtra("viewName",context.getString(R.string.person_center_list_item_text_renew));
        Bundle bundle = new Bundle();
        bundle.putSerializable("remoteBean",bean);
        intent.putExtra("remote",bundle);
        context.startActivity(intent);
    }

    private void initListener() {
        select_num.setNumChangeListener(new SelectNumLayout.OnNumChangeListener() {
            @Override
            public void onChange(int num) {
                mNum = num;
                MyLog.e("num = " + num);
                String s = String.valueOf(num * selectPayInfo.price);
                MyLog.e("order price = " + s);
                tv_order_price.setText(s);
            }
        });
        btn_pay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                payPresenter.queryOrder(new IUIDataListener() {
                    @Override
                    public void uiDataError(Exception exc) {

                    }

                    @Override
                    public void uiDataSuccess(Object obj) {
                        CommSmallLoadingDialog.showDialog(PayActivity.this,"获取订单...");
                        BaseResultWrapper<String> wrapper = (BaseResultWrapper<String>) obj;
                        String orderNo = wrapper.data;
                        Intent intent = new Intent(PayActivity.this, OrderActivity.class);
                        intent.putExtra("orderPrice", tv_order_price.getText().toString());
                        intent.putExtra("priceType", selectPayInfo.name);
                        intent.putExtra("payNum", select_num.getNum());
                        intent.putExtra("id", selectPayInfo.id);
                        intent.putExtra("orderNo",orderNo);
                        PayActivity.this.startActivity(intent);
                    }
                },mNum);

            }
        });
    }

    private void initView() {
        mGridview = findViewById(R.id.gridview);
        select_num = findViewById(R.id.select_num);
        tv_order_price = findViewById(R.id.tv_order_price);
        btn_pay = findViewById(R.id.btn_pay);
        inc_top_view = findViewById(R.id.inc_top_view);
        ll_renew = findViewById(R.id.ll_renew);
        v_renew = findViewById(R.id.v_renew);
        tv_renew_name = findViewById(R.id.tv_renew_name);
    }

    private void initData() {
        Intent intent = getIntent();
        String viewName = intent.getStringExtra("viewName");
        if (!TextUtils.isEmpty(viewName)) {
            inc_top_view.setName(viewName);
            if (viewName.equals(getString(R.string.person_center_list_item_text_renew))){
                ll_renew.setVisibility(View.VISIBLE);
                v_renew.setVisibility(View.VISIBLE);
                Bundle bundle = intent.getBundleExtra("remote");
                RemoteConnectBean remoteBean = (RemoteConnectBean) bundle.getSerializable("remoteBean");
                String string = getString(R.string.go_renew);
                tv_renew_name.setText(string + remoteBean.getName());
            }
        }
        payPresenter = new PayPresenter(this);
        mAdapter = new BaseAdapter() {
            @Override
            public int getCount() {
                return data == null ? 0 : data.size();
            }

            @Override
            public Object getItem(int position) {
                return null;
            }

            @Override
            public long getItemId(int position) {
                return 0;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                if (convertView == null) {
                    convertView = new PayItem(PayActivity.this);
                    PayItem root = (PayItem) convertView;
                    PayInfo payInfo = data.get(position);
                    root.setTag(payInfo);
                    root.setData(payInfo);
                    if (position == 0) {
                        selectPayInfo = payInfo;
                        root.setChecked(true);
                    } else {
                        root.setChecked(false);
                    }

                    setOrderPrice();
                    root.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            PayInfo info = (PayInfo) v.getTag();
                            selectPayInfo = info;
                            EventBus.getDefault().post(new MyEvent.PayItemCheckEvent(info));
                            setOrderPrice();
                        }
                    });
                }
                return convertView;
            }
        };

        mGridview.setAdapter(mAdapter);
        payPresenter.start();
        CommSmallLoadingDialog.showDialog(this, getString(R.string.request_ing));
        payPresenter.load();
    }

    public void setOrderPrice() {
        String string = String.valueOf(select_num.getNum() * selectPayInfo.price);
        MyLog.e("setOrderPrice price = " + string);
        tv_order_price.setText(string);
    }

    @Override
    public Context getContext() {
        return null;
    }

    @Override
    public void success(List<PayInfo> data) {
        MyLog.e("success data size = " + data);
        if (data == null){
            return;
        }
        this.data.clear();
        this.data.addAll(data);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void logout() {
        ToastUtil.showToast("已在其他设备登录！");

        finish();
    }

    @Override
    public void setPresenter(PayActivityContract.IPresenter iPresenter) {
        this.mIPresenter = iPresenter;

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe
    public void handle(MyEvent.PaySuccessEvent event) {
        finish();
    }


    @Subscribe
    public void onEventMainThread(final MyEvent.paySuccess event) {
        finish();
    }
}
